package com.mach.platform.handlers;

import org.joda.time.DateTime;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.handlers
 * @time 2018/9/13 0013 22:45
 */
public class EchartFactory {

	private       Map<String, Object> producton;
	private final String              KEY_LEGEND = "legend";
	private final String              KEY_XDATA  = "xaxis";
	private final String              KEY_YDATA  = "yaxis";
	private final String              KEY_TITLE  = "title";
	private final String              MAP_YEAR  = "fyear";
	private final String              MAP_MONTH  = "fmonth";
	private final String              MAP_DAY  = "fday";

	private String    timeType;
	private String    date;
	private List<Map> actConsume;
	private List<Map> planConsume;
	private LinkedList yDatas = new LinkedList();
	private List<Integer> legendNums = null;

	public EchartFactory (String timeType, String date, List<Map> actConsume, List<Map> planConsume) {
		this.timeType = timeType;
		this.date = date;
		this.actConsume = actConsume;
		this.planConsume = planConsume;
		producton = new HashMap<>();
		producton.put(KEY_YDATA, yDatas);
	}

	public Map<String, Object> work ( ) {

		makeXdata();
		makeYdatas(actConsume,true);
		makeYdatas(planConsume,false);

		return producton;
	}

	private void makeYdatas (List<Map> targetList, boolean ifFirst) {

			// 先处理 没有数据的情况
		if (targetList.size() == 0) {
			if (ifFirst) {
				yDatas.addFirst(Collections.EMPTY_LIST);
			} else {
				yDatas.addLast(Collections.EMPTY_LIST);
			}
			return;
		}

		List<Object> objectList = null;

		// 处理 map中要获取的key名称
		String targetMapKeyName = "全年份".equals(timeType) ? MAP_YEAR : ( "年度".equals(timeType) ? MAP_MONTH : MAP_DAY );

		// 开始处理
		// 判断 数据 是否 跟 x 轴数据  一致长度，以x轴为标准，没有对应的x轴，设为0
		if (legendNums.size() == targetList.size()) {

			// sorted 排序，map取出数值，toList成集合
			objectList = targetList.stream().sorted(
					Comparator.comparingInt(o -> Integer.parseInt(o.get(targetMapKeyName).toString()))).map(item -> item.get("num")).collect(Collectors.toList());

		} else {

			// 处理 长度不一致的情况，对应的位置设置为0
			// 1. 先排序, 从低到高
			List<Map> sortedResult = targetList.stream().sorted(
					Comparator.comparingInt(o -> Integer.parseInt(o.get(targetMapKeyName).toString()))).collect(Collectors.toList());

			List<Integer> compareList = sortedResult.stream().map(ite -> Integer.parseInt(ite.get(targetMapKeyName).toString()))
			                                        .collect(Collectors.toList());

			// 遍历，缺失的位置补0
			for (int i = 0; i < legendNums.size(); i++) {

				Integer currentTimeFlag = legendNums.get(i);
				if (!compareList.contains(currentTimeFlag)) {
					Map newMap = new HashMap();
					newMap.put("num", 0);
					if (i < compareList.size()) {
						sortedResult.add(i, newMap);
					} else {
						sortedResult.add(newMap);
					}
				}
			}

			// 执行添加
			objectList = sortedResult.stream().map(item -> item.get("num")).collect(Collectors.toList());


		}

		if (ifFirst) {
			yDatas.addFirst(objectList);
		} else {
			yDatas.addLast(objectList);
		}

	}





	public void makeXdata ( ) {

		List<String> values = null;

		switch (timeType) {
			case "全年份":

				if (actConsume.size() == 0 && planConsume.size() == 0) {
					values = Collections.emptyList();
					break;
				}
				// 获取长度较长的
				legendNums = Stream.concat(actConsume.stream(), planConsume.stream())
				                                  .map(item -> Integer.parseInt(item.get("fyear").toString())).distinct()
				                                  .sorted((o1, o2) -> o1-o2).collect(Collectors.toList());

				 values=legendNums.stream().map(item -> item + "年").collect(Collectors.toList());

				producton.put(KEY_TITLE, timeType + "碳排走势");
				break;

			case "年度":
				legendNums= Stream.iterate(1, num -> num + 1).limit(12).collect(Collectors.toList());
				values= legendNums.stream().map(item -> item + "月").collect(Collectors.toList());
				producton.put(KEY_TITLE, date.substring(0, 4) + "碳排走势");
				break;
			case "月度":
				legendNums=Stream.iterate(1, num -> num + 1).limit(DateTime.parse(date).dayOfMonth().getMaximumValue()).collect(Collectors.toList());
				values= legendNums.stream().map(item -> item + "日").collect(Collectors.toList());
				producton.put(KEY_TITLE, date.substring(5, 7) + "碳排走势");
				break;
			case "日度":

				break;
			default:
				break;
		}

		producton.put(KEY_XDATA, values);
		producton.put(KEY_LEGEND, new String[]{"实际碳排","计划碳排"});

	}


}
